import MainHeader from '@/components/main-header/index'
import GetWeather from '@/apis/GetWeather'
import sunny from '@/static/weather-logo/sunny.svg'
import cloudy from '@/static/weather-logo/cloudy.svg'
import lighting from '@/static/weather-logo/lighting.svg'
import rain from '@/static/weather-logo/rain.svg'
import fog from '@/static/weather-logo/fog.svg'
import overcast from '@/static/weather-logo/overcast.svg'
import snow from '@/static/weather-logo/snow.svg'
import AnnouncementQueryByMobile from '@/apis/AnnouncementQueryByMobile'
import just from '@/static/notification/just.svg'
import normal from '@/static/notification/normal.svg'
import important from '@/static/notification/important.svg'

export default {
  name: 'Homepage',
  components: {
    MainHeader,
  },
  mounted() {
    // 天气预报
    const _this = this
    uni.showLoading({
      title: '加载中',
    })
    uni.getLocation({
      type: 'wgs84',
      success: function(res) {
        GetWeather(
          {
            longitude: res.longitude,
            latitude: res.latitude,
          },
          _this.$store.state.authorization
        )
          .then((res) => {
            const info = JSON.parse(res.data.data).lives
            _this.weather = info[0]
            // console.log(_this.weather)
            const weatherType = {
              rain,
              cloudy,
              lighting,
              sunny,
              fog,
              overcast,
              snow,
            }
            const weatherText = info[0].weather
            _this.weatherPics = weatherText.includes('雨')
              ? weatherType.rain
              : weatherText.includes('云')
              ? weatherType.cloudy
              : weatherText.includes('雷')
              ? weatherType.lighting
              : weatherText.includes('晴')
              ? weatherType.sunny
              : weatherText.includes('雾')
              ? weatherType.fog
              : weatherText.includes('阴')
              ? weatherType.overcast
              : weatherText.includes('雪')
              ? weatherType.snow
              : weatherText.includes('霾')
              ? weatherType.fog
              : ''

            uni.hideLoading()
          })
          .catch((err) => {
            console.log(err)
          })
      },
    })
    // 公告
    AnnouncementQueryByMobile(this.$store.state.authorization)
      .then((res) => {
        let response = res.data.data
        if (res.data.flag === true) {
          response.forEach((item) => {
            item.createTime = this.formateDate(item.createTime)
          })
          this.notificationList = response
        } else {
          //
        }
      })
      .catch((err) => {
        console.log(err)
      })
  },
  data() {
    return {
      weather: {
        city: '',
        weather: '',
        winddirection: '',
        windpower: '',
        humidity: '',
        temperature: '',
      },
      weatherPics: '',
      notificationList: '',
    }
  },
  methods: {
    goTo(goal) {
      uni.navigateTo({
        url: goal,
      })
    },
    selectLogo(level) {
      return level === '紧急'
        ? important
        : level === '一般'
        ? normal
        : level === '重要'
        ? just
        : null
      ;('@/static/notification/just.svg')
    },
    displayNotification(item) {
      // 跳转并传输 item
      const data = JSON.stringify(item)
      uni.navigateTo({
        url:
          '/pages/main/components/homepage/display-notification/index?data=' +
          data,
      })
    },
    formateDate(date) {
      return (
        date.split(' ')[0].split('-')[0] +
        '-' +
        date.split(' ')[0].split('-')[1] +
        '-' +
        date.split(' ')[0].split('-')[2] +
        ' ' +
        date.split(' ')[1].split(':')[0] +
        ':' +
        date.split(' ')[1].split(':')[1]
      )
    },
  },
}
